$(document).ready(function() {
    var showChar = 100;
    var ellipsestext = "...";
    var moretext = "m&#225;s";
    var lesstext = "menos";

    //Shorten long messages
    $('.more').each(function() {
        var content = $(this).html();

        if (content.length > showChar) {

            var c = content.substr(0, showChar);
            var h = content.substr(showChar, content.length - showChar);

            var html = c + '<span class="moreellipses">' + ellipsestext + '</span><span class="morecontent"><span>' + h + '</span>&nbsp;<a href="" class="morelink">' + moretext + '</a></span>';

            $(this).html(html);
        }

    });

    //Enlarge shortened message
    $(".morelink").click(function() {
        if ($(this).hasClass("less")) {
            $(this).removeClass("less");
            $(this).html(moretext);
            $(this).prev().hide();
        } else {
            $(this).addClass("less");
            $(this).html(lesstext);
            $(this).prev().show("fast");
        }
        $(this).parent().prev().toggle();
        return false;
    });
    
    //Disable guardar button and input fields at the beginning
    $("#perfilForm\\:guardarButton").prop("disabled", true);
    $("#perfilForm input").prop("disabled", true);
    $("#perfilForm\\:modifyButton").prop("disabled", false);

    //Enable editing fields in perfil after clicking modificar
    $("#perfilForm\\:modifyButton").click(function() {
        $("input").not(".unchangable").prop("disabled", false);
        $(this).prop("disabled", true);
    });
    
    //Disable recipients when writing quejas
    $(".messageTypes input[type='radio']").change(function() {
        if ($(this).attr("value") == "queja") {
            $("#newMessageForm\\:messageRecipientsMenu").prop("disabled", true);
        } else {
            $("#newMessageForm\\:messageRecipientsMenu").prop("disabled", false);
        }
    });
});